package cn.yunhe.invalidate;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;


/**
 * @author 孙敖
 * @date 2018/9/20 15:24
 */
public class MoneyValidator implements ConstraintValidator<CustomMoney, Double> {

   private String moneyReg = "^\\d+(\\.\\d{1,2})?$";//表示金额的正则表达式
   private Pattern moneyPattern = Pattern.compile(moneyReg);

   public void initialize(CustomMoney constraint) {


   }

   @Override
   public boolean isValid(Double value, ConstraintValidatorContext arg) {
      if (value==null)
         return true;
      return moneyPattern.matcher(value.toString()).matches();
   }


}
